import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/utils/module_permissions_util.dart';
import 'package:enterprise_v6_app/app/data/entities/application/securtity_base_drawer.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/double_control_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/hazard_investigation_task_management/hazard_task_magt_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/hidden_danger_control_page/hidden_danger_control_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/home/home_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/investigation_plan_ledger/investigation_plan_ledger_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/task_allocation_hazard_investigation/task_allocation_hazard_investigation_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/double_prevention_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
/// 隐患排查
class DangerInvestigationController extends GetxController {
  /// 外部controller
  final applyController = Get.find<DoublePreventionController>();

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 展开收起控制key
  final scaffoldKey = GlobalKey<ScaffoldState>();

  /// 选中的页面
  int selectIndex = 0;
  var filterWidget = Rx<Widget>(Container());
  @override
  void onInit() {
    super.onInit();
    initModule();
  }

  /// 根据后台权限初始化菜单
  void initModule() {
    final roleMenuList = applyController.roleMenuMap[AppRoutes.hiddenDiseaseSearch];
    ModulePermissionsUtil.removeRoleMenu(titleList: titleList, menuTreeList: roleMenuList);
    update();
  }

  final titleList = [
    SecurityBaseEntity(
      title: '总览',
      icon: AppImages.icon_risk_overview,
      expandTitle: '总览',
      currentPageTitle: 0,
      pages: [DangerInvestigationHomePage()],
      path: '/home',
    ),
    SecurityBaseEntity(
      title: '任务信息管理',
      icon: AppImages.icon_hazard_investigation_task_management,
      expandTitle: '任务信息管理',
      currentPageTitle: 0,
      pages: [HazardInvestigationTaskManagementPage()],
      path: '/taskManage',
    ),
    SecurityBaseEntity(
      title: '任务分配',
      icon: AppImages.icon_task_allocation_hazard_investigation,
      expandTitle: '任务分配',
      currentPageTitle: 0,
      pages: [TaskAllocationHazardInvestigationPage()],
      path: '/taskInvestigation',
    ),
    SecurityBaseEntity(
      title: '排查计划台账',
      icon: AppImages.icon_investigation_plan_ledger,
      expandTitle: '排查计划台账',
      currentPageTitle: 0,
      pages: [InvestigationPlanLedgerPage()],
      path: '/planLedger',
    ),
    SecurityBaseEntity(
      title: '隐患治理台账',
      icon: AppImages.icon_hidden_danger_control,
      expandTitle: '隐患治理台账',
      currentPageTitle: 0,
      pages: [HiddenDangerControlPage()],
      path: '/dangerLedger',
    ),
  ];

  /// title选择切换
  /// [titleIndex] 标题索引
  /// [index] 页面索引
  void selectTitle(int titleIndex, int index) {
    titleList[titleIndex].currentPageTitle = index;
    update();
  }

  /// 点击左侧菜单
  /// [index] 索引
  void selectLeftMenu(int index) {
    selectIndex = index;
    update();
  }

  /// 点击展开菜单
  /// [index] 索引
  void selectExpandMenu(int index) {
    selectIndex = index;
    Get.back();
    update();
  }

  /// 筛选列表数据
  List<DoubleControlEntity> doubleControlList = [];

  /// 获取安全风险辨识结构树
  void _getSafeTree() async {
    final res = await HttpUtil().generateGetRequest<List<DoubleControlEntity>>(Interface.etRiskIdentificationTree).execute();

    res.onSuccess((result) {
      if (result.data != null) {
        doubleControlList = result.data!;
        update();
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
    _getSafeTree();
  }
}
